package chaoyue.study.safe;

/**
 * ThreadLocal 本质上是提供一个Map集合
 * 用来绑定线程对象和数据 方便数据进行传递（隐式传参）
 * 类似线程域的上下文
 */
public class ThreadLocalTest1 {
    // 创建ThreadLocal
    public static final ThreadLocal<String> local1 = new ThreadLocal<>();
    public static final ThreadLocal<Integer> local2 = new ThreadLocal<>();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            local1.set("chaoyue");
            local2.set(233);
            getDataFromLocal();
        });

        Thread t2 = new Thread(() -> {
            local1.set("olivia");
            local2.set(18);
            getDataFromLocal();
        });

        t1.start();
        t2.start();
    }

    private static void getDataFromLocal() {
        System.out.println(Thread.currentThread().getName() + ": " + local1.get() + "-" + local2.get());
    }
}
